'use strict'

var _mm = require('util/mm.js');
var _cites = require('util/cites/index.js');
var _address = require('service/address-service.js');
var templateAddressModel = require('./address-model.string');

var addressModel = {
    show: function (option) {
        //option绑定
        this.option = option;
        this.option.data = option.data || {};
        this.$modelWrap = $('.model-wrap');
        //渲染
        this.loadModel();
        //绑定
        this.bindEvent();
    },
    loadModel: function () {
        var addressModelHtml = _mm.renderHtml(templateAddressModel, {
            isUpdate: this.option.isUpdate,
            data: this.option.data
        });
        this.$modelWrap.html(addressModelHtml);
        //加载省份
        this.loadProvince();
    },
    //加载省份信息
    loadProvince: function () {
        var provinces = _cites.getProvinces() || [],
            $provinceSelect = this.$modelWrap.find('#receiver-province');
        $provinceSelect.html(this.getSelectOption(provinces));
        //如果是更新地址，且有省份信息，做省份的回填
        if (this.option.isUpdate && this.option.data.receiverProvince) {
            $provinceSelect.val(this.option.data.receiverProvince);
            this.loadCites(this.option.data.receiverProvince);
        }
    },
    //加载城市信息
    loadCites: function (provinceName) {
        var cities = _cites.getCites(provinceName) || [],
            $citySelect = this.$modelWrap.find('#receiver-city');
        $citySelect.html(this.getSelectOption(cities));
        //如果是更新地址，且有城市信息，做城市的回填
        if (this.option.isUpdate && this.option.data.receiverCity) {
            $citySelect.val(this.option.data.receiverCity);
        }
    },
    getSelectOption: function (optionArray) {
        var html = '<option value="">请选择</option>';
        for (var i = 0, length = optionArray.length; i < length; i++) {
            html += '<option value="' + optionArray[i] + '">' + optionArray[i] + '</option>';
        }
        return html;
    },
    bindEvent: function () {
        var _this = this;
        //省市二级联动
        this.$modelWrap.find('#receiver-province').change(function () {
            var selectProvince = $(this).val();
            _this.loadCites(selectProvince);
        });
        //点击叉号关闭弹窗
        this.$modelWrap.find('.close').click(function () {
            _this.hide();
        });
        //点击内容区不关闭弹窗
        this.$modelWrap.find('.model-container').click(function (e) {
            e.stopPropagation();
        });
        //提交收货地址
        this.$modelWrap.find('.address-btn').click(function () {
            var receiverInfo = _this.getReceiverInfo(),
                isUpdate = _this.option.isUpdate;
            //使用新地址，并且验证通过
            if (!isUpdate && receiverInfo.status) {
                _address.save(receiverInfo.data, function (res) {
                    _mm.successTips('地址添加成功');
                    _this.hide();
                    typeof _this.option.onSuccess === 'function' && _this.option.onSuccess(res);
                }, function (errMsg) {
                    _mm.errorTips(errMsg);
                });
            } else if (isUpdate && receiverInfo.status) {
                _address.update(receiverInfo.data, function (res) {
                    _mm.successTips('地址更新成功');
                    _this.hide();
                    typeof _this.option.onSuccess === 'function' && _this.option.onSuccess(res);
                }, function (errMsg) {
                    _mm.errorTips(errMsg);
                });
            } else {
                _mm.errorTips(receiverInfo.errMsg || '哪里不对了');
            }
        });
    },
    getReceiverInfo: function () {
        var receiverInfo = {},
            result = {
                status: false
            };
        receiverInfo.receiverName = $.trim(this.$modelWrap.find("#receiver-name").val());
        receiverInfo.receiverProvince = this.$modelWrap.find("#receiver-province").val();
        receiverInfo.receiverCity = this.$modelWrap.find("#receiver-city").val();
        receiverInfo.receiverAddress = $.trim(this.$modelWrap.find("#receiver-address").val());
        receiverInfo.receiverPhone = $.trim(this.$modelWrap.find("#receiver-phone").val());
        receiverInfo.receiverZip = $.trim(this.$modelWrap.find("#receiver-zip").val());
        if (this.option.isUpdate) {
            receiverInfo.id = this.$modelWrap.find('#receiver-id').val();
        }
        //表单验证
        if (!receiverInfo.receiverName) {
            result.errMsg = '请输入收件人姓名';
        } else if (!receiverInfo.receiverProvince) {
            result.errMsg = '请选择收件人所在省份';
        } else if (!receiverInfo.receiverCity) {
            result.errMsg = '请选择收件人所在城市';
        } else if (!receiverInfo.receiverAddress) {
            result.errMsg = '请输入收件人详细地址';
        } else if (!receiverInfo.receiverPhone) {
            result.errMsg = '请输入收件人手机号';
        } else {
            result.status = true;
            result.data = receiverInfo;
        }
        return result;
    },
    //关闭弹窗
    hide: function () {
        this.$modelWrap.empty();
    }
};
module.exports = addressModel;
